package com.siruier.boss.api.core

import android.util.Log
import com.didi.drouter.api.DRouter
import com.siruier.boss.router.service.IBaseUrlService
import com.squareup.moshi.FromJson
import com.squareup.moshi.Moshi
import com.squareup.moshi.ToJson
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import okhttp3.ConnectionPool
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.io.IOException
import java.math.BigDecimal
import java.net.Proxy
import java.util.concurrent.TimeUnit


val MOSHI = Moshi.Builder()
    .add(BigDecimalTypeAdapter())
    .add(KotlinJsonAdapterFactory())
    .build()


class BigDecimalTypeAdapter {

    @FromJson
    @Throws(IOException::class)
    fun fromJson(value: String): BigDecimal {
        return BigDecimal(value)
    }

    @ToJson
    fun toJson(value: BigDecimal?): String? {
        return value?.toPlainString()
    }
}

/**
 *   @auther : zhoucl
 *   time   : 2017/09/04
 */
object ApiHelper {
    private const val TIME_OUT = 60L * 2
    private var BASE_URL: String? = null
    private var CHANNEL: String = ""
    private var INTERCEPTOR: Interceptor? = null
    private var SHOW_LOGGER: Boolean = true
    private val mRetrofit: Retrofit by lazy {
        Retrofit.Builder()
            .client(getOkHttpClient(TIME_OUT))
            .addConverterFactory(MoshiConverterFactory.create(MOSHI))
//            .addConverterFactory(FastJsonConverterFactory.create())
//            .addCallAdapterFactory(CoreCallAdapterFactory.create(WrapResultBean::class.java))
            .baseUrl(getBaseUrl())
            .build()
    }

    private val mBaseUrlService: IBaseUrlService by lazy {
        DRouter.build(IBaseUrlService::class.java).getService()
    }

    fun initHttp(
        baseUrl: String,
        channel: String,
        interceptor: Interceptor,
        showLogger: Boolean = true
    ) {
        BASE_URL = baseUrl
        CHANNEL = channel
        INTERCEPTOR = interceptor
        SHOW_LOGGER = showLogger
    }

    fun getBaseUrl() = BASE_URL ?: mBaseUrlService.getBaseUrl()

    fun getChannel() = CHANNEL

    fun <T> create(clazz: Class<T>): T = mRetrofit.create(clazz)

    private fun getOkHttpClient(timeout: Long): OkHttpClient {
        return OkHttpClient.Builder()
            .connectTimeout(timeout, TimeUnit.SECONDS)
            .writeTimeout(timeout, TimeUnit.SECONDS)
            .readTimeout(timeout, TimeUnit.SECONDS)
            .proxy(Proxy.NO_PROXY)
            .apply {
                INTERCEPTOR?.let { addInterceptor(it) }
                if (SHOW_LOGGER) {
                    addInterceptor(getHttpLoggingInterceptor())
                }
            }
            .connectionPool(ConnectionPool(8, 15, TimeUnit.SECONDS))
            .build()
    }

    private fun getHttpLoggingInterceptor() =
        HttpLoggingInterceptor {
            Log.e("Logger", "Api请求==>$it")
        }.apply {
            setLevel(HttpLoggingInterceptor.Level.BODY)
        }
}